package com.work.controller;/**
 * @author： li
 * @date： 2021-06-01 10:08
 * @version 1.0
 */

import com.work.dao.userdao;
import com.work.pojo.user;
import com.work.service.userservice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author： Administrator
 * @date： 2021-06-01 10:08
 * @version 1.0
 */
@WebServlet("*.user")
public class userServlet extends HttpServlet {
    public userServlet() {
        super();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("GBK");
        String url=req.getServletPath();
        if(url.equals("/registermail.user")){
            String email=req.getParameter("email");
            String password=req.getParameter("password");
            user us=new user();
            us.setEmail(email);
            us.setPassword(password);
            us.setCookies(java.util.UUID.randomUUID().toString());
            userservice use=new userservice();
           int i=use.register(us);
           if(i>0){
               req.setAttribute("msg","注册成功！");
           }
           else {
               req.setAttribute("msg","注册失败！");
           }
           /*String cookies=null;
                   cookies=us.getActcode();
           req.setAttribute("cookies",cookies);
           req.getRequestDispatcher("/cookies.mail").forward(req,resp);*/
            req.getRequestDispatcher("/register.jsp").forward(req, resp);
        }
        else if(url.equals("/loginmail.user")){
            String email=req.getParameter("email");
            String password=req.getParameter("password");
            user us=new user();
            us.setEmail(email);
            us.setPassword(password);
            userservice use=new userservice();
            int i=0;
            try {
                i=use.login(us);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if(i>0){
                ResultSet rs=null;
                String cookies=null;
                try {
                    rs = userdao.selectbyusername(us);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                try {
                    if(rs.next()){
                    try {
                       cookies=rs.getString("cookies");
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                req.setAttribute("cookies",cookies);
                req.setAttribute("email",email);
                //System.out.println("userservlet的cookies"+cookies);
                req.getRequestDispatcher("/email.mail").forward(req,resp);
        }
            else {
                req.setAttribute("msg", "邮箱账号或密码错误，登入失败！");
                req.getRequestDispatcher("/index.jsp").forward(req,resp);
            }
        }
    }

    @Override
    public void destroy() {
        super.destroy();
    }
}
